package com.atguigu.flink.day09;

import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @author Felix
 * @date 2024/8/19
 * 该案例演示了算子状态-列表状态
 * 需求：在process算子中的每个并行度上计算数据的个数
 */
public class Flink07_OpeState_ListState_2 {
    public static void main(String[] args) throws Exception {
        //TODO 1.环境准备
        //1.1 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(2);
        //开启检查点
        env.enableCheckpointing(5000L);
        //TODO 2.从指定的网络端口读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.在process算子中的每个并行度上计算数据的个数
        socketDS.process(
                new MyPro()
        ).print();


        env.execute();

    }
}

class MyPro extends  ProcessFunction<String, String> implements CheckpointedFunction{

    //用于存储每个并行子任务计算元素的个数
    Integer count = 0;

    //定义一个算子状态 用于对成员变量进行备份以及恢复
    ListState<Integer> countListState;

    @Override
    public void snapshotState(FunctionSnapshotContext context) throws Exception {
        System.out.println("~~~snapshotState~~~");
        countListState.clear();
        countListState.add(count);
    }

    @Override
    public void initializeState(FunctionInitializationContext context) throws Exception {
        System.out.println("~~~initializeState~~~");
        //注意：算子状态和键控状态不一样
        //键控状态的初始化是在open方法中完成的 getRuntimeContext.getXXXState()
        //算子状态的初始化是在initializeState方法中完成的
        ListStateDescriptor<Integer> listStateDescriptor
                = new ListStateDescriptor<Integer>("countListState", Integer.class);
        countListState = context.getOperatorStateStore().getListState(listStateDescriptor);

        if(context.isRestored()){
            count = countListState.get().iterator().next();
        }
    }

    @Override
    public void processElement(String value, ProcessFunction<String, String>.Context ctx, Collector<String> out) throws Exception {
        out.collect("当前子任务"+getRuntimeContext().getIndexOfThisSubtask()+"计算的元素的个数为" + ++count);
    }
}